
let domainConfig =
    [
        {
            "domainId": "1111",
            "url": "https://lookerstudio.google.com/u/0/reporting/f458ffbc-4c05-40fc-8014-d4ee83486baf/page/p_no29gpuqbd",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "1"
                }
            },
            "lookerId": "f458ffbc-4c05-40fc-8014-d4ee83486baf"
        },
        {
            "domainId": "1134",
            "url": "https://lookerstudio.google.com/reporting/6f6d3499-e9ea-40c3-87e9-204c69690c29/page/JWojD",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "6f6d3499-e9ea-40c3-87e9-204c69690c29"
        },
        {
            "domainId": "1062",
            "url": "https://lookerstudio.google.com/u/0/reporting/bf27b79f-54e5-45ef-b676-00cfd5c08fc5/page/clcWD",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "bf27b79f-54e5-45ef-b676-00cfd5c08fc5"
        },
        {
            "domainId": "1016",
            "url": "https://lookerstudio.google.com/reporting/f76758ee-7059-4833-997f-a2978b832c49/page/Ecz1C",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "1"
                },
                "gaCart": {
                    "type": "doubleColumn",
                    "index": "5"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "4"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "3"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "7"
                }
            },
            "lookerId": "f76758ee-7059-4833-997f-a2978b832c49"
        },
        {
            "domainId": "1032",
            "url": "https://lookerstudio.google.com/u/0/reporting/7d9af8eb-595c-4522-8a0c-86f04c8f4800/page/E9JWD",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "7d9af8eb-595c-4522-8a0c-86f04c8f4800"
        },
        {
            "domainId": "1148",
            "url": "https://lookerstudio.google.com/reporting/737e32a6-ae14-4370-aa0f-45d762f4d821/page/dBZmD",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "737e32a6-ae14-4370-aa0f-45d762f4d821"
        },
        {
            "domainId": "1160",
            "url": "https://lookerstudio.google.com/u/0/reporting/0d76d801-16aa-4c16-a208-760c8e2d229b/page/p_no29gpuqbd",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "1"
                }
            },
            "lookerId": "0d76d801-16aa-4c16-a208-760c8e2d229b"
        },
        {
            "domainId": "1171",
            "url": "https://lookerstudio.google.com/u/0/reporting/9d0503a1-5745-428d-8d47-4b1f878015e9/page/JWojDhttps://lookerstudio.google.com/u/0/reporting/9d0503a1-5745-428d-8d47-4b1f878015e9/page/JWojD",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "9d0503a1-5745-428d-8d47-4b1f878015e9"
        },
        {
            "domainId": "1166",
            "url": "https://lookerstudio.google.com/u/0/reporting/f81a59a6-83e3-4eee-b886-38753eaf856f/page/JWojD",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "f81a59a6-83e3-4eee-b886-38753eaf856f"
        },
        {
            "domainId": "1172",
            "url": "https://lookerstudio.google.com/u/0/reporting/0914d6ba-a356-4473-ba2a-f47d10a301c6/page/JWojD",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "0914d6ba-a356-4473-ba2a-f47d10a301c6"
        },
        {
            "domainId": "e632f6187785493eb28f67ed5c39f12e",
            "url": "https://lookerstudio.google.com/u/0/reporting/af934b36-c259-4db0-a8f3-39cecbcfd239/page/b1N3D",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "9"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "af934b36-c259-4db0-a8f3-39cecbcfd239"
        },
        {
            "domainId": "c8419ceadd834db7a8973229a77b84cb",
            "url": "https://lookerstudio.google.com/reporting/8d362a85-6851-4ca5-a841-20189671adda/page/p2N3D",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "8d362a85-6851-4ca5-a841-20189671adda"
        },
        {
            "domainId": "94d1c2a2cb2346fd8a6eae407b3c99cf",
            "url": "https://lookerstudio.google.com/reporting/2abbdee3-62bf-4dd2-b1c1-4f0225ecffd2/page/p2N3D",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "2abbdee3-62bf-4dd2-b1c1-4f0225ecffd2"
        },
        {
            "domainId": "1024",
            "url": "https://lookerstudio.google.com/reporting/e5cf24bd-5c60-4a85-8c97-2ba70d0991c7/page/p2N3D",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "e5cf24bd-5c60-4a85-8c97-2ba70d0991c7"
        },
        {
            "domainId": "1026",
            "url": "https://lookerstudio.google.com/reporting/034c3d1a-f7e6-4a6a-949e-1162699097c2/page/p2N3D",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "034c3d1a-f7e6-4a6a-949e-1162699097c2"
        },
        {
            "domainId": "1025",
            "url": "https://lookerstudio.google.com/reporting/d9120459-8a59-47fb-929d-d8221e11ee3f/page/p2N3D",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "7"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "8"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "d9120459-8a59-47fb-929d-d8221e11ee3f"
        },
        {
            "domainId": "1235",
            "url": "https://lookerstudio.google.com/reporting/5bb1fbc3-6a2e-4fc8-84fe-3fd57f2ff7fb/",
            "dataMapping": {
                "day": {
                    "type": "dateColumn",
                    "index": "0"
                },
                "gaCart": {
                    "type": "doubleColumn"
                },
                "gaPays": {
                    "type": "doubleColumn",
                    "index": "3"
                },
                "gaOrderAmount": {
                    "type": "doubleColumn",
                    "index": "4"
                },
                "gaClicks": {
                    "type": "doubleColumn",
                    "index": "2"
                }
            },
            "lookerId": "5bb1fbc3-6a2e-4fc8-84fe-3fd57f2ff7fb"
        }
    ]
async function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
const postMessageString = (config, type, message) => {
    chrome.runtime.sendMessage({ greeting: "站点" + config.domainId + message, type }, function (response) {
    });
}
const lookerstudioInit = async (config) => {
    postMessageString(config, "succes", "begin init")
    const dateEle = document.getElementsByClassName("lego-control md-button md-data-studio-theme md-ink-ripple")[0]
    dateEle.click()
    await sleep(1000)
    const rightTio = document.getElementsByClassName("ng-pristine ng-untouched ng-valid md-data-studio-theme ng-empty")[0]
    rightTio.click()
    await sleep(1000)
    const seven = document.getElementsByClassName("md-data-studio-theme md-ink-ripple")[3]
    seven.click()
    const submit = document.getElementsByClassName("md-button md-raised")[0]
    submit.click()
    await sleep(4000)
    postMessageString(config, "succes", "end init")
}
const lookerStudioRun = async () => {
    const config = domainConfig.find((row) => window.location.href.indexOf(row.lookerId) != -1)
    if (!config) {
        postMessageString({ domainId: "0" }, "error", "config is null")
        return
    }
    try {
        // if(config.domainId =="1235") {
        //     return
        // }
        // await lookerstudioInit(config)
        const params = []
        const table = document.getElementsByClassName("centerColsContainer")[document.getElementsByClassName("centerColsContainer").length - 1]
        if (!table) {
            postMessageString(config, "error", "table is null")
            return
        }
        postMessageString(config, "succes", "begin parse html")
        table.childNodes.forEach((row, index) => {
            if (index === 0) return
            const rows = {}
            row.childNodes.forEach((item, index) => {
                if (index === 0) return
                if (index == Number(config.dataMapping.day.index)) {
                    let date = new Date(item.childNodes[0].innerText.replaceAll("年", "-").replaceAll("月", "-").replaceAll("日", ""))
                    rows['day'] = formatDate(date)
                }
                if (index == Number(config.dataMapping.gaPays.index)) {
                    rows['gaPays'] = item.childNodes[0].innerText.replace(/[^0-9.]/g, '')
                }
                if (index == Number(config.dataMapping.gaOrderAmount.index)) {
                    rows['gaOrderAmount'] = Number(item.childNodes[0].innerText.replace(/[^0-9.]/g, '')) * 100
                }
                if (index == Number(config.dataMapping.gaClicks.index)) {
                    rows['gaClicks'] = item.childNodes[0].innerText.replace(/[^0-9.]/g, '')
                }
                rows['mongodbId'] = config.domainId + "_" + rows.day
                rows['domainId'] = config.domainId
            })
            if (Object.keys(rows).length > 0) params.push(rows)
        })
        postMessageString(config, "succes", "parse end is ok")
        const headers = new Headers();
        headers.append('Content-Type', 'application/json');
        fetch('http://localhost:8082/dataservice/data/saveOrUpdateMongo', {
            method: 'POST',
            headers: headers,
            body: JSON.stringify(params)
        })
            .then(response => {
                return response.json()
            }).then(data => {
                postMessageString(config, "succes", "pull data is succes")
            })
            .catch(error => {
                postMessageString(config, "error", error)
            });
    } catch (error) {
        postMessageString(config, "error", error)
    }
}